package com.ifighter.activity;

import com.ifighter.R;
import com.ifighter.helper.MyDbHelper;
import com.ifighter.service.cluster.Cluster;
import com.ifighter.service.cluster.ClusterService;

import android.app.Activity;
import android.os.Bundle;

import java.sql.SQLException;
import java.util.ArrayList;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class SignUpActivity extends Activity{

	public MyDbHelper myDbHelper;
	private SQLiteDatabase db;
	public MyCustomAdapter dataAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signup_2);

		displayListView();
		checkButtonClick();
	}

	private void displayListView() {

		ArrayList<Cluster> elements = null;
		Cluster[] clusters          = null;
		ClusterService cService     = null;

		myDbHelper = new MyDbHelper(SignUpActivity.this);
		db         = this.myDbHelper.getReadableDatabase();
		elements   = new ArrayList<Cluster>();
		cService   = new ClusterService(db);

		try {
			clusters = cService.getTeamByName();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < clusters.length; i++) {
			elements.add(clusters[i]);
		}

		//create an ArrayAdaptar from the String Array
		dataAdapter = new MyCustomAdapter(this,R.layout.cluster_info, elements);
		ListView listView = (ListView) findViewById(R.id.listView1);
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);


		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				Cluster country = (Cluster) parent.getItemAtPosition(position);
				Toast.makeText(getApplicationContext(),
						"Clicked on Row: " + country.getsType(), 
						Toast.LENGTH_LONG).show();
			}
		});

	}

	private class MyCustomAdapter extends ArrayAdapter<Cluster> {

		private ArrayList<Cluster> countryList;

		public MyCustomAdapter(Context context, int textViewResourceId, 
				ArrayList<Cluster> countryList) {
			super(context, textViewResourceId, countryList);
			this.countryList = new ArrayList<Cluster>();
			this.countryList.addAll(countryList);
		}

		private class ViewHolder {
			TextView code;
			CheckBox name;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			Log.v("ConvertView", String.valueOf(position));

			if (convertView == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
				convertView = vi.inflate(R.layout.cluster_info, null);

				holder = new ViewHolder();
				holder.code = (TextView) convertView.findViewById(R.id.code);
				holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
				convertView.setTag(holder);

				holder.name.setOnClickListener( new View.OnClickListener() {  
					public void onClick(View v) {  
						CheckBox cb = (CheckBox) v ;  
						Cluster country = (Cluster) cb.getTag();  
						Toast.makeText(getApplicationContext(),
								"Clicked on Checkbox: " + cb.getText() +
								" is " + cb.isChecked(), 
								Toast.LENGTH_LONG).show();
						country.setSelected(cb.isChecked());
					}  
				});  
			} 
			else {
				holder = (ViewHolder) convertView.getTag();
			}

			Cluster country = countryList.get(position);
			holder.name.setText(country.getsType());
			holder.name.setChecked(country.isSelected());
			holder.name.setTag(country);

			return convertView;

		}

	}

	private void checkButtonClick() {


		Button myButton = (Button) findViewById(R.id.findSelected);
		myButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				StringBuffer responseText = new StringBuffer();
				responseText.append("The following were selected...\n");

				ArrayList<Cluster> countryList = dataAdapter.countryList;
				for(int i=0;i<countryList.size();i++){
					Cluster country = countryList.get(i);
					if(country.isSelected()){
						responseText.append("\n" + country.getsType());
					}
				}

				Toast.makeText(getApplicationContext(),
						responseText, Toast.LENGTH_LONG).show();
			}
		});
	}
}

